export function setCookie(name, value) {
  const Days = 30; // 此 cookie 将被保存 30 天
  const exp = new Date(); // new Date('December 31, 9998');
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  document.cookie = `${name}=${escape(value)};expires=${exp.toGMTString()};path=/`; // + Config.content;
}

export function getCookie(name) {
  const search = `${name}=`; // 查询检索的值
  let returnvalue = ''; // 返回值
  if (document.cookie.length > 0) {
    let sd = document.cookie.indexOf(search);
    if (sd != -1) {
      sd += search.length;
      let end = document.cookie.indexOf(';', sd);
      if (end == -1) end = document.cookie.length;
      // returnvalue = unescape(document.cookie.substring(sd, end));
      returnvalue = document.cookie.substring(sd, end);
    }
  }
  return decodeURI(returnvalue);
}

export function getToken() {
  const map2forum_token = getCookie('map2forum_token')
  let token
  if (window.location.host.includes('meix.com')) {
    token = getCookie('pcToken')
  } else {
    token = getCookie('token')
  }
  // console.log('map2forum_token', map2forum_token, 'token', token)
  let iawebToken
  if (map2forum_token != null && map2forum_token != 'null' && map2forum_token != '') {
    iawebToken = map2forum_token
  } else {
    iawebToken = token
  }
  if (iawebToken) {
    return iawebToken;
  }
  if (process.env.NODE_ENV === 'production') {
    return null;
  }
  return '';
}

export function encode(str) {
  // 对字符串进行编码
  const encode = encodeURI(str);
  // 对编码的字符串转化base64
  const base64 = btoa(encode);
  return base64;
}
